<?php

    /**
     * @package      Modules
     * @subpackage   Modules_Setup
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Modules_Setup_Installer extends Base_Setup_Installer
    {

    ////////////////////////////////////////////////////////////////////////////
    //                               Attributes                               //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Package slug
         *
         * @var     string
         */
        protected $_package = 'default';

        /**
         * Show module
         *
         * @var     string
         */
        protected $_display = true;

        /**
         * Module Db object
         *
         * @var     DbModule
         */
        protected $_module = null;

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Installs the module
         *
         * @throws       Core_Exception
         * @return       void
         */
        public function run()
        {
            // Gets the module paths
            $setupPath      = explode('_', get_class($this));
            $moduleName     = $setupPath[0];

            // Adds a record to core_module table
            try {
                $module                             = new DbModule();
                $module->status                     = DbBase::ENABLED;
                $package                            = Doctrine::getTable('DbPackage')->findOneBySlug($this->_package);

                if (!$package) {
                    $package                        = new DbPackage();
                    $package->slug                  = $this->_package;
                    $package->save();
                }

                $module->DbPackage                  = $package;
                $module->slug                       = strtolower($moduleName);
                $module->display                    = $this->_display;

                $module->save();

                $this->_module = $module;

            } catch (Exception $e) {
                throw new Core_Exception("Fatal error: unable to add module information to database : " . $e->getTraceAsString());
            }
        }

        /**
         * Post install
         *
         * @throws       Core_Exception
         * @return       void
         */
        public function postInstall()
        {
            if ($this->_module !== null && $this->_module instanceof DbModule) {
                $this->_module->DbProfile->merge(
                              Doctrine_Query::create()
                                 ->from('DbProfile')
                                 ->where('slug = ?', 'admin')
                                 ->execute()
                             );

                $this->_module->save();
            }
        }
    }
